package com.module.lemlin.owner

import android.content.res.Resources
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.annotation.ColorRes
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.KeyboardUtils
import com.blankj.utilcode.util.ToastUtils
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

abstract class OwnerAbstractActivity : AppCompatActivity() {

    /**
     * 需要绑定的视图ID
     */
    abstract val layoutResId: Int

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (layoutResId != 0) {
            setContentView(layoutResId)
        }
        EventBus.getDefault().register((this))
        this.init()
    }

    override fun onStop() {
        super.onStop()
        //隐藏软键盘
        if (KeyboardUtils.isSoftInputVisible((this@OwnerAbstractActivity)))
            KeyboardUtils.hideSoftInput((this@OwnerAbstractActivity))
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister((this))
    }

//    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
//    override fun attachBaseContext(newBase: Context?) {
//        val newConfig = newBase?.resources?.configuration
//        if (newConfig != null) {
//            newConfig.fontScale = 1f
//            val context = createConfigurationContext(newConfig)
//            super.attachBaseContext(context)
//        } else {
//            super.attachBaseContext(newBase)
//        }
//    }

    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    override fun getResources(): Resources {
        val resources = super.getResources()
        val newConfig = resources.configuration
        if (newConfig.fontScale != 1f) {
            newConfig.fontScale = 1f
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
//                createConfigurationContext(newConfig)
//            } else {
//                resources.updateConfiguration(newConfig, resources.displayMetrics)
//            }
            return createConfigurationContext(newConfig).resources
        }
        return super.getResources()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun onEventMainThread(any: Any) {
    }

    /**
     * 初始化操作
     * */
    private fun init() {
        this.initStatusBar()
        this.doBeforeInit()
        this.initView()
        this.initData()
    }

    /**
     * 设置沉浸式状态栏
     * */
    private fun initStatusBar() {
        //设置状态栏模式
        BarUtils.setStatusBarLightMode(
            (this), stateBar().isLightMode
        )
        //设置状态栏颜色
        BarUtils.setStatusBarColor(
            (this), ColorUtils.getColor(stateBar().color)
        )
        //为标题栏添加状态栏的高度
        stateBar().view?.let {
            BarUtils.addMarginTopEqualStatusBarHeight(it)
        }
        //设置底部菜单栏颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            BarUtils.setNavBarColor(this.window, ColorUtils.getColor(stateBar().bottomColor))
        }
    }

    /**
     * 初始化视图和数据之前进行操作
     */
    open fun doBeforeInit() = Unit

    /**
     * 初始化视图
     */
    open fun initView() = Unit

    /**
     * 初始化数据
     */
    open fun initData() = Unit

    /**
     * toast提示
     * */
    fun toast(message: String?) {
        ToastUtils.showShort(message ?: "未知错误")
    }

    /**
     *
     * */
    open fun stateBar(): StatusBar = StatusBar()

    data class StatusBar(
        var isLightMode: Boolean = true,
        @ColorRes var color: Int = android.R.color.transparent,
        @ColorRes var bottomColor: Int = android.R.color.white,
        var view: View? = null
    )
}